<template>
  <div>
    <my-breadcrumb :items="['消息管理', '评论管理']"/>
    <search :formItems="searchItems" :check="true" @search="getList" ref="search"/>
    <el-table :data="comments" @sort-change="orderByTime" :border="true">
      <el-table-column
        type="index"
        width="55">
      </el-table-column>
      <el-table-column
        type="selection"
        width="55">
      </el-table-column>
      <el-table-column
        label="用户名称"
        prop="membername">
      </el-table-column>
      <el-table-column
        label="评论时间"
        width="150px"
        sortable="custom"
        prop="createtime">
      </el-table-column>
      <el-table-column
        label="评论内容"
        prop="content">
      </el-table-column>
      <el-table-column
        label="点赞量"
        sortable="custom"
        width="90px"
        prop="fabulous">
      </el-table-column>
      <el-table-column
        label="评论类型"
        prop="type">
        <template slot-scope="scope">
          {{ scope.row.type == '1' ? '资讯' : (scope.row.type == '2' ? '帖子' : (scope.row.type == '3' ? '求购' :  (scope.row.type == '4' ? '评论' : 'repo'))) }}
        </template>
      </el-table-column>
      <el-table-column
        label="对应内容ID"
        prop="parid">
      </el-table-column>
      <el-table-column
        label="删除原因"
        prop="delcontent">
      </el-table-column>
      <el-table-column
        label="操作"
        width="180px">
        <template slot-scope="scope">
          <!--<el-button size="mini" type="primary">编辑</el-button>-->
          <el-button size="mini" v-if="scope.row.delflag === '0'" type="text" @click="toDelete(scope.row.id, scope.row.memberid, scope.row.content)">删除</el-button>
          <el-button size="mini" v-else type="text" disabled>已删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="page">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.pageNum"
        :page-sizes="[20, 50, 100]"
        :page-size="page.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  name: 'comment',
  data () {
    return {
      searchItems: [
        {
          label: '用户名称',
          type: 'input',
          key: 'membername'
        },
        {
          label: '评论时间',
          type: 'times',
          key: 'times'
        },
        {
          label: '评论类型',
          type: 'select',
          key: 'type',
          options: [{id: '1', name: '资讯'}, {id: '2', name: '帖子'}, {id: '3', name: '求购'}, {id: '5', name: 'repo'}]
        },
        {
          label: '评论内容',
          type: 'input',
          key: 'content'
        },
        {
          label: '对应内容ID',
          width: '100px',
          type: 'input',
          key: 'parid'
        }
        // ,
        // {
        //   label: '最低点赞量',
        //   type: 'input',
        //   width: '100',
        //   key: 'fabulous1'
        // },
        // {
        //   label: '最高点赞量',
        //   type: 'input',
        //   width: '100',
        //   key: 'fabulous2'
        // }
      ], // 检索结构
      comments: [],
      page: {
        pageNum: 1,
        pageSize: 20,
        total: 0
      },
      lifting: null
    }
  },
  methods: {
    // /Comment/index
    getList (page = 1) {
      let searchData = {delflag: '0'}
      if (this.$refs['search']) {
        searchData = this.$refs['search'].search()
      }
      // if (searchData.fabulous1 && !searchData.fabulous2) {
      //   searchData.fabulous = searchData.fabulous1 + ' and 99999999999'
      // } else if (!searchData.fabulous1 && searchData.fabulous2) {
      //   searchData.fabulous = '0 and ' + searchData.fabulous2
      // } else if (searchData.fabulous1 && searchData.fabulous2) {
      //   searchData.fabulous = searchData.fabulous1 + ' and ' + searchData.fabulous2
      // }
      let params = {...searchData, page: page, size: this.page.pageSize, orderby: this.lifting}
      this.axios.get('/Comment/index', {params})
        .then(res => {
          if (res.data.status) {
            this.page.total = Number(res.data.data.count)
            this.comments = res.data.data.list
            this.page.pageNum = page
          }
        })
    },
    handleSizeChange (e) {
      this.page.pageSize = e
      this.getList()
    },
    handleCurrentChange (e) {
      this.page.pageNum = e
      this.getList(e)
    },
    orderByTime (lifting) {
      if (lifting.order === 'ascending') {
        this.lifting = lifting.prop + ' asc'
      } else {
        this.lifting = lifting.prop + ' desc'
      }
      this.getList()
    },

    toDelete (id, memberId, content) {
      this.$prompt('请输入删除原因', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then(({ value }) => {
        this.axios.get('/Comment/delcomment', {params: {id, memberid: memberId, delcontent: value, content}})
          .then(res => {
            if (res.data.status === 1) {
              this.getList()
              this.$message.success(res.data.msg)
            }
          })
      })
    }
  },
  created () {
    this.getList()
  }
}
</script>

<style scoped>

</style>
